//********************************************************************************************
//
// File : tcp.h implement for Transmission Control Protocol
//
//********************************************************************************************
//
// Copyright (C) 2007
//
// This program is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free Software
// Foundation; either version 2 of the License, or (at your option) any later
// version.
// This program is distributed in the hope that it will be useful, but
//
// WITHOUT ANY WARRANTY;
//
// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
// PURPOSE. See the GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along with
// this program; if not, write to the Free Software Foundation, Inc., 51
// Franklin St, Fifth Floor, Boston, MA 02110, USA
//
// http://www.gnu.de/gpl-ger.html
//
//********************************************************************************************
#ifndef __TCP_h__
#define __TCP_h__

#include "BasicTypes.h"

class cTcp
{
public:
    static const U8 TCP_HEADER_LEN      = 20;
    static const U8 TCP_OPTION_LEN      = 4;
    static const U8 TCP_FLAG_FIN_V      = 0x01;
    static const U8 TCP_FLAG_SYN_V      = 0x02;
    static const U8 TCP_FLAG_RST_V      = 0x04;
    static const U8 TCP_FLAG_PSH_V      = 0x08;
    static const U8 TCP_FLAG_ACK_V      = 0x10;
    static const U8 TCP_FLAG_URG_V      = 0x20;
    static const U8 TCP_FLAG_ECE_V      = 0x40;
    static const U8 TCP_FLAG_CWR_V      = 0x80;
    static const U8 TCP_SRC_PORT_H_P    = 0x22;
    static const U8 TCP_SRC_PORT_L_P    = 0x23;
    static const U8 TCP_DST_PORT_H_P    = 0x24;
    static const U8 TCP_DST_PORT_L_P    = 0x25;
    static const U8 TCP_SEQ_P           = 0x26;   // the tcp seq number is 4 bytes 0x26-0x29
    static const U8 TCP_SEQACK_P        = 0x2A;   // 4 bytes
    static const U8 TCP_HEADER_LEN_P    = 0x2E;
    static const U8 TCP_FLAGS_P         = 0x2F;
    static const U8 TCP_WINDOWSIZE_H_P  = 0x30;   // 2 bytes
    static const U8 TCP_WINDOWSIZE_L_P  = 0x31;
    static const U8 TCP_CHECKSUM_H_P    = 0x32;
    static const U8 TCP_CHECKSUM_L_P    = 0x33;
    static const U8 TCP_URGENT_PTR_H_P  = 0x34;   // 2 bytes
    static const U8 TCP_URGENT_PTR_L_P  = 0x35;
    static const U8 TCP_OPTIONS_P       = 0x36;
    static const U8 TCP_DATA_P          = 0x36;

//********************************************************************************************
//
// Prototype function
//
//********************************************************************************************
public:
    static U8 seqnum;

    static U16 tcp_get_dlength ( U8 *rxtx_buffer );
    static U8 tcp_get_hlength ( U8 *rxtx_buffer );
    static U16 tcp_puts_data ( U8 *rxtx_buffer, U8 *data, U16 offset );

    static void tcp_send_packet (
        U8 *rxtx_buffer,
        U16 dest_port,
        U16 src_port,
        U8 flags,
        U8 max_segment_size,
        U8 clear_seqack,
        U16 next_ack_num,
        U16 dlength,
        U8 *dest_mac,
        U8 *dest_ip );

    static bool TCP_process ( U8 *rxtx_buffer, U8 *dest_mac, U8 *dest_ip );
};

#endif      //#ifndef __TCP_h__


